--
-- @Author:      feilong
-- @DateTime:    2019-12-19 15:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local act_def = require("act_def")

local table_insert = table.insert
local table_remove = table.remove
local math_floor = math.floor

local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)

	self.message_dispatch = message_dispatch
	


	self:register()
end

--注册本服务里的消息
function MessageHandler:register()
	--启动
	self.message_dispatch:registerSelf('start', handler(self,self.start))

	self.message_dispatch:registerSelf('get_activity_list', handler(self,self.getActList))
	self.message_dispatch:registerSelf('reload_config', handler(self,self.reloadConfig))
	self.message_dispatch:registerSelf('nt_user_act_change', handler(self,self.ntUserActivityChange))
	

	self.message_dispatch:registerAll(handler(self,self.dispatchToActivity)) --具体活动的消息会透传到具体的活动


end

------------------------------------------------------------------
--cmd
------------------------------------------------------------------
function MessageHandler:start()
	dbglog("______manager_start_______")
	self.config = global.config
	self.act_manager = global.act_manager
	self.config:start()
	self.act_manager:start()
	
end

--消息转发
function MessageHandler:dispatchToActivity(cmd, msg)
	local act_id = msg.act_id
	if act_id and act_id > 0 then 
		local act = self.act_manager:getAct(act_id)
		local res = nil
		if act then 
			res = act:callMessage(cmd, msg)
		else
			errlog("_____no__act_service___", act_id)
		end
		return res
	end
	print("_______222_______", cmd, msg)
	local act_type = msg.act_type
	if act_type then --传到指定类型的活动中
		local acts = self.act_manager:getActByType(act_type)
		for k, act in pairs(acts) do 
			act:sendMessage(cmd, msg)
		end
	end
	
end

--取活动列表
function MessageHandler:getActList(uid)
    dbglog("get_activity_list========aa",uid)
	local now = os.time()
    local data_list = {}
    local act_list = self.act_manager:getCurActList()
    -- dbglog("______getCurActList__", act_list)
    local act_data = nil
	for act_id, act in pairs(act_list) do		
		act_data = act:getData(uid)
		if act_data then 
	        table_insert(data_list, act_data )		
	    end
	end
    dbglog("get_activity_list============end",data_list)
	return  {activity_list = data_list}
end


function MessageHandler:reloadConfig(msg)
	dbglog("reloadConfig========活动配置发生了改变")
    --活动配置发生了改变
    self.config:reload()
    self.act_manager:checkActivity()
    self.act_manager:ntActivityChange()
end

--活动状态发生了改变通知指定用户
function MessageHandler:ntUserActivityChange(uid)
	if not uid or uid < 0 then 
		return 
	end
	dbglog("ntUserActivityChange========活动状态发生了改变")
    self.act_manager:ntUserActivityChange(uid)

end

-- --为websvr 做的一个替补热更
-- function MessageHandler:websvrHotfix()
-- 	local ok,ret = R().websvr():send(14, 'CTRL', 'set_cache', true)	
-- 	print("______id____ok_____ret")

-- 	local ok,ret = R().websvr():send(15, 'CTRL', 'set_cache', true)	
-- 	print("______id____ok_____ret")
-- end
















return MessageHandler